import Http from '@/axios';
import { ApiDictType, ApiDict, ApiRole } from '@/axios/api';
import { Dict, DictType, Result } from '@/model/common';
import { DictUtil } from '@/util';

export default {
    dict: (context: any, dictTypes: DictType[]) => {
        const requestList: Promise<Result>[] = [];
        dictTypes.forEach((type) => {
            switch (type) {
                case DictType.DICT_TYPE:
                    requestList.push(ApiDictType.listDict());
                    break;
                case DictType.ROLE:
                    requestList.push(ApiRole.listDict());
                    break;
                case DictType.USER_STATUS:
                    requestList.push(ApiDict.listDict(type));
                    break;
            }
        });
        if (requestList.length > 0) {
            Http.all(requestList).then((res: any) => {
                for (const [index, item] of res.entries()) {
                    context.commit('dict', { type: dictTypes[index], data: DictUtil.trans(item) });
                }
            });
        }
    },
};
